﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.Data;
using Jumpton.Persistent;
using System.Data;
using Jumpton.SFA.Server.Entity;

namespace Jumpton.SFA.Server.BusinessRule.Objective
{
    public class ObjectiveManager : BusinessLogic
    {
        public ObjectiveManager()
        { }

        public Objective_Details LoadDetail(int ID)
        {
            return (Objective_Details)ObjectRow.Load(typeof(Objective_Details), string.Format(@"this.ID={0}", ID));
        }

        public ObjectTable SelectObjectiveDetailList(decimal DomainID, string where)
        {
            return new ObjectTable(typeof(Jumpton.SFA.Server.Entity.Objective_Details), string.Format(" DOMAIN_ID={0} {1}", DomainID,where ));
        }
        public ObjectTable SelectObjectiveDetailViewList(decimal DomainID, string where)
        {
            return new ObjectTable(typeof(Jumpton.SFA.Server.Entity.ObjectiveDetailView), string.Format(" o.DOMAIN_ID={0} {1}", DomainID, where));
        }

        public ObjectTable SelectObjectiveDetailList(decimal DomainID, string orgid, string keyid, string where)
        {
            return new ObjectTable(typeof(Jumpton.SFA.Server.Entity.Objective_Details), string.Format(" DOMAIN_ID={0} AND KEYID='{1}' AND OBJECTID IN( SELECT ID FROM USERS WHERE DOMAIN_ID={2} AND ORG_ID={3} ) {4}", DomainID, keyid,DomainID,orgid,where));
        }

        public Jumpton.SFA.Server.Entity.Objective LoadObjective(int ID)
        {
            return (Jumpton.SFA.Server.Entity.Objective)ObjectRow.Load(typeof(Jumpton.SFA.Server.Entity.Objective), string.Format(@"this.ID={0}",ID));
        }

        public Jumpton.SFA.Server.Entity.Objective LoadObjective(decimal DomainID,string Type)
        {
            return (Jumpton.SFA.Server.Entity.Objective)ObjectRow.Load(typeof(Jumpton.SFA.Server.Entity.Objective), string.Format(@"DOMAIN_ID={0} AND OBJECTIVETYPE='{1}'", DomainID,Type));
        }

        public ObjectTable SelectObjectiveList(decimal DomainID, string WhereCondition)
        {
            return new ObjectTable(typeof(Jumpton.SFA.Server.Entity.Objective), string.Format(" DOMAIN_ID={0} {1}", DomainID, WhereCondition));
        }
        
        public DataSet SelectObjectiveDetail(decimal DomainID, string WhereCondition, DateTime date)
        {
            return Jumpton.Data.SqlData.OpenSP("sp_GetObjectiveDetailList", DomainID, WhereCondition, date);
        }


        public bool ExistsObjectiveDeatils(string oid, string date)
        {
            return (Jumpton.SFA.Server.Entity.Objective_Details)ObjectRow.Load(typeof(Jumpton.SFA.Server.Entity.Objective_Details), string.Format(@"OBJECTIVE_ID='{0}'  AND YEAR_MONTH='{1'} ", oid,date)) == null ? false : true; 
        }
       
        public bool ExistsObjectiveDeatils(string userid, string date,string oid)
        {
            return (Jumpton.SFA.Server.Entity.Objective_Details)ObjectRow.Load(typeof(Jumpton.SFA.Server.Entity.Objective_Details), string.Format(@"OBJECTID='{0}' AND YEAR_MONTH='{1}' AND OBJECTIVE_ID='{2}' ", userid, date, oid)) == null ? false : true;
        }
        public bool ExistsObjectiveName(string Name, decimal DomainID)
        {
            return (Jumpton.SFA.Server.Entity.Objective)ObjectRow.Load(typeof(Jumpton.SFA.Server.Entity.Objective), string.Format(@"this.Name='{0}' AND DOMAIN_ID={1}", Name, DomainID))==null?false:true;

            
        }
        public bool ExistsObjectiveType(string Type, decimal DomainID)
        {
            return (Jumpton.SFA.Server.Entity.Objective)ObjectRow.Load(typeof(Jumpton.SFA.Server.Entity.Objective), string.Format(@"this.OBJECTIVETYPE='{0}' AND DOMAIN_ID={1}", Type, DomainID)) == null ? false : true;


        }
        public Jumpton.SFA.Server.Entity.Objective LoadForType(string Type, decimal DomainID)
        {
            return (Jumpton.SFA.Server.Entity.Objective)ObjectRow.Load(typeof(Jumpton.SFA.Server.Entity.Objective), string.Format(@"this.OBJECTIVETYPE='{0}' AND DOMAIN_ID='{1}'", Type, DomainID));
        }

        public bool Save(Entity.Objective objective)
        {
            if (objective != null)
            {
                try
                {
                    objective.Save();
                    return true;
                }
                catch (Exception err)
                {
                    message = err.Message; 
                    return false;
                }
                
            }

            return false;
        }

        public bool Save(Entity.Objective_Details Objectivedetails)
        {
            if (Objectivedetails != null)
            {
                try
                {
                    Objectivedetails.Save();
                    return true;
                }
                catch (Exception err)
                {
                    message = err.Message;
                    return false;
                }

            }

            return false;
        }



    }
}
